<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/

/**
 * Get all tags
 * @returns array
 * @return array of zero or more tags, or NULL on failure
 * @param id integer
 * @param tag string
 * @param timestamp integer looking for greater or minus
 * @param status integer tag moderated or not
 * @param uid integer
 * @param name string Forum name
 * @todo Support 'cids' and 'fids' arrays, 
 */

function conversations_userapi_gettags($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $ctags = $xartable['ctags'];

    $query = "SELECT DISTINCT xar_id, xar_tag, xar_timestamp,"
            . " xar_lastvisit, xar_status, xar_uid, xar_count, "
            . " xar_options "
            . " FROM $ctags";

    $where = array();
    $bind = array();

    if (!empty($id)) {
        $where[] = 'xar_id = ?';
        $bind[] = (int)$id;
    }

    if (!empty($tag)) {
        $where[] = 'xar_tag = ?';
        $bind[] = (string)$tag;
    }

    if (!empty($timestamp)) {
        $where[] = 'xar_timestamp > ?';
        $bind[] = (int)$timestamp;
    }

    if (!empty($lastvisit)) {
        $where[] = 'xar_lastvisit = ?';
        $bind[] = (int)$lastvisit;
    }
    
    if (!empty($status)) {
        $where[] = 'xar_status = ?';
        $bind[] = (int)$status;
    }

    if (!empty($uid)) {
        $where[] = 'xar_uid = ?';
        $bind[] = (int)$uid;
    }
    
    if (!empty($count)) {
        $where[] = 'xar_count > ?';
        $bind[] = (int)$count;
    }

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY xar_id";

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($id, $tag, $timestamp, $lastvisit, $status, $uid, $count, $options) = $result->fields;

        $tags[] = array(
                'id'        => $id,
                'tag'       => xarVarPrepForDisplay($tag),
                'timestamp' => $timestamp,
                'lastvisit' => $lastvisit,
                'status'    => $status,
                'uid'       => $uid,
                'count'     => $count//,
                //'options'   => unserialize($options)
        );
    }

    $result->Close();
    return $tags;
}
?>